/**
 *    Copyright (C) 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_DYNVECTOR_PAGES_DATA_PAGE_HPP
#define	_CUBEFS_CORE_DYNVECTOR_PAGES_DATA_PAGE_HPP

#include <cubefs/core/tools/reflection.hpp>
#include <cubefs/core/container/container.hpp>

namespace cubefs    {
namespace dynvector {

using namespace cubefs::tools;
using namespace cubefs::ctr;

#pragma pack(1)

template <
        typename Profile,
        typename ComponentList,
        template <Int> class DataBlockTypeFactory,
        typename BaseType
>
class DataPage: public PageBuilder<Profile, ComponentList, BaseType>
{

public:

    typedef DataPage<
                Profile,
                ComponentList,
                DataBlockTypeFactory,
                BaseType
    >                                                                           Me;

    typedef PageBuilder<Profile, ComponentList, BaseType>                       Base;

    typedef typename BaseType::PageMgr                                          PageMgr;

public:
    
    typedef typename DataBlockTypeFactory<PageMgr::PAGE_SIZE - sizeof(Base)>::Type PageDataType;

private:

    PageDataType data_;

    static PageMetadata *reflection_;

public:

    DataPage(): Base(), data_() {}

    const PageDataType& data() const {
        return data_;
    }

    PageDataType& data() {
        return data_;
    }

    static Int hash() {
        return reflection_->Hash();
    }

    static bool is_abi_compatible() {
        return reflection_->IsAbiCompatible();
    }

    static PageMetadata *reflection() {
        return reflection_;
    }

    void Reindex()
    {
        Base::Reindex();
        data_.reindex();
    }

    Int data_size() const
    {
        Me* me = NULL;
        return ((Int)(BigInt)&me->data_) + data_.byte_size();
    }

    static Int get_max_size() {
        return PageDataType::max_size();
    }

    void BuildFieldsList(MetadataList &list, Long &abi_ptr) const {
        Base::BuildFieldsList(list, abi_ptr);
        FieldFactory<PageDataType>::create(list, data(), "DATA", abi_ptr);
    }

    static Int get_page_size(const void* buf) {
        const Me* me = static_cast<const Me*>(buf);
        return me->data_size();
    }

    static Int Init() {

        if (reflection_ == NULL)
        {
            Long abi_ptr = 0;
            Me* me = 0;
            MetadataList list;
            me->BuildFieldsList(list, abi_ptr);

            Int hash0 = 1234567;
            Int attrs = BITMAP;

            reflection_ = new PageMetadataImpl("DATA_PAGE", list, attrs, hash0, &get_page_size, PageMgr::PAGE_SIZE);
        }
        else {}

        return reflection_->Hash();
    }

    void *operator new(size_t size, PageMgr &mgr) {
        return mgr.create_new();
    }

    void operator delete(void *buf, size_t size) {}
};


template <
        typename Profile,
        typename ComponentList,
        template <Int> class DataBlockTypeFactory,
        typename BaseType
>
PageMetadata* DataPage<Profile, ComponentList, DataBlockTypeFactory, BaseType>::reflection_ = NULL;

#pragma pack()

}
}

#endif
